const path = require('path')
const Router = require("koa-router")
const router = new Router({ prefix: '/upload' })


router.post('/', (ctx) => {
  const { file } = ctx.request.files
  const fileTypes = ['image/jpeg', 'image/png']
  if (file) {
    if (!fileTypes.includes(file.mimetype)) {
      ctx.body = {
        code: 10502,
        message: '图片上传格式错误',
        result: ''
      }
      return
    }
    ctx.body = {
      code: 0,
      message: '图片上传成功',
      result: {
        goods_img: path.basename(file.filepath)
      }
    }
  } else {
    ctx.body = {
      code: 10501,
      message: '图片上传失败',
      result: ''
    }
  }
})


module.exports = router